<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="title">
            <h2><a href="#" class="back" onclick="history.go(-1)"/>Pesquisar Cliente</h2>
        </ui:define>

        <ui:define name="body">
            <p:panel styleClass="container_24 clearfix">
                <div class="buttonAction" style="float: right">
                    <p:button value="Adicionar" href="criarcliente.xhtml" icon="ui-icon-plusthick"
                              styleClass="ui-priority-primary"/>
                </div>
            </p:panel>

            <h:form id="clienteForm" enctype="multipart/form-data">            
                <p:messages id="messages" closable="true" showDetail="true" autoUpdate="true" />
                <p:panel id="dadosGeraisPanel" styleClass="container_24 clearfix">
                    <h2>Filtros</h2>

                    <p:outputLabel id="personOutputLabel" value="Pessoa:" styleClass="grid_3" style="top: 6px" />
                    <div class="grid_8">
                        <p:selectOneRadio id="personRadioButton" value="#{managerPesquisarCliente.cliente.tipoDocumento}" >
                            <f:selectItems value="#{managerPesquisarCliente.tipoDocumento}" />
                            <p:ajax event="change" update="@form" />
                        </p:selectOneRadio>
                    </div>
                    <div class="clear"/>

                    <p:outputLabel value="#{managerPesquisarCliente.pessoaFisica ? 'CPF:' : 'CNPJ:'}" styleClass="grid_3"/>
                    <p:inputMask value="#{managerPesquisarCliente.pessoaFisica ? managerPesquisarCliente.cliente.cpf : managerPesquisarCliente.cliente.cnpj}" 
                                 styleClass="grid_8" mask="#{managerPesquisarCliente.pessoaFisica ? '999.999.999-99' : '99.999.999/9999-99'}" 
                                 autocomplete="false" />
                    <div class="clear"/>

                    <p:outputLabel value="#{managerPesquisarCliente.pessoaFisica ? 'Nome:' : 'Nome Fantasia:'}" 
                                   styleClass="grid_3 #{managerPesquisarCliente.pessoaFisica ? '' : 'nomeFantasiaLabel'}" 
                                   for="nomeInputText"/>
                    <p:inputText id="nomeInputText" 
                                 value="#{managerPesquisarCliente.pessoaFisica ? managerPesquisarCliente.cliente.nome : managerPesquisarCliente.cliente.nomeFantasia}" 
                                 styleClass="grid_20"/>
                    <div class="clear"/>

                    <p:outputLabel value="RG:" styleClass="grid_3" 
                                   rendered="#{managerPesquisarCliente.pessoaFisica}"/> 
                    <p:inputText styleClass="grid_8" value="#{managerPesquisarCliente.cliente.rg}" 
                                 rendered="#{managerPesquisarCliente.pessoaFisica}"/>
                    <div class="clear"/>

                    <h2>Bancos</h2>

                    <p:outputLabel value="Banco:" styleClass="grid_3" for="tipoBancoSelectOneMenu" />
                    <div class="grid_20" style="margin-left: 2px">
                        <p:selectOneMenu id="tipoBancoSelectOneMenu" 
                                         styleClass="grid_8"
                                         value="#{managerPesquisarCliente.banco.tipoBanco}" >
                            <f:selectItem itemLabel="" itemValue="" />
                            <f:selectItems value="#{managerPesquisarCliente.tipoBanco}" />
                        </p:selectOneMenu>
                    </div>
                    <div class="clear" />

                    <p:outputLabel value="Agência:" styleClass="grid_3" for="agInputText" />
                    <p:inputText id="agInputText" styleClass="grid_8" value="#{managerPesquisarCliente.banco.agencia}" />

                    <p:outputLabel value="Conta:" styleClass="grid_3" for="contaInputText" />
                    <p:inputText id="contaInputText" styleClass="grid_9" value="#{managerPesquisarCliente.banco.conta}" />

                    <div class="clear" />
                    <br />

                    <div class="buttonAction">
                        <p:commandButton value="Pesquisar" styleClass="ui-priority-primary"
                                         actionListener="#{managerPesquisarCliente.pesquisar()}"
                                         update=":clienteForm:clientesPanel, :clienteForm:clientesTable"
                                         id="pesquisarCommandButton" icon="ui-icon-search"/>
                    </div>
                </p:panel> 

                <p:panel id="clientesPanel" styleClass="container_24 clearfix">
                    <p:dataTable id="clientesTable" var="item" value="#{managerPesquisarCliente.clientes}" 
                                 styleClass="grid_24" emptyMessage="Nenhum registro">  
                        <f:facet name="header">
                            Clientes
                        </f:facet>

                        <p:column headerText="Ação" width="50">
                            <p:button title="Editar"
                                      href="editarcliente.xhtml?eCliente=#{item.id}"
                                      icon="ui-icon-pencil"/>
                            <p:button title="Visualizar"
                                      href="visualizarcliente.xhtml?vCliente=#{item.id}"
                                      icon="ui-icon-zoomin"/>
                            <p:commandButton update="@form" title="Excluir"
                                             oncomplete="dlg.show()"
                                             icon="ui-icon-trash" >
                                <f:setPropertyActionListener target="#{managerPesquisarCliente.clienteDelete}" value="#{item}" />
                            </p:commandButton>
                        </p:column>

                        <p:column headerText="Nome">  
                            #{item.tipoDocumento eq 'CPF' ? item.nome : item.nomeFantasia}
                        </p:column>

                        <p:column headerText="Agência">  
                            <ui:repeat var="ag" value="#{item.bancos}">
                                #{ag.agencia}
                            </ui:repeat>
                        </p:column> 

                        <p:column headerText="Conta">  
                            <ui:repeat var="ag" value="#{item.bancos}">
                                #{ag.conta}
                            </ui:repeat>
                        </p:column> 

                    </p:dataTable>

                    <div class="buttonAction">
                        <p:button value="Cancelar" icon="ui-icon-circle-close"
                                  href="indexcliente.xhtml"/>
                    </div>

                </p:panel>

                <p:dialog id="modalDialog" header="Confirmação de remoção" widgetVar="dlg" modal="true" 
                          height="80" closable="true" resizable="false" >
                    <h:outputText value="Tem certeza que deseja remover este cliente?" />
                    <div class="buttonAction">
                        <p:commandButton value="Sim"
                                         actionListener="#{managerPesquisarCliente.deletar()}"
                                         update="@form"
                                         icon="ui-icon-check"/>
                        <p:commandButton value="Não" icon="ui-icon-circle-close"
                                         oncomplete="dlg.hide()" />

                    </div>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>
</html>


